package com.tao.com.xiaowen.module.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mobads.AdView;
import com.baidu.mobads.AdViewListener;
import com.baidu.mobads.AppActivity;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.tao.com.xiaowen.config.DataUntils;
import com.tao.com.xiaowen.config.ToastUtil;
import com.tao.com.xiaowen.model.JokeData;
import com.tao.com.xiaowen.model.Model;
import com.tao.com.xiaowen.module.news.NewsContentActivity;
import com.tao.com.xiaowen.adapter.NewsFragmentAdapter;
import com.tao.com.xiaowen.model.News;
import com.tao.com.xiaowen.R;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by Administrator on 2016/8/1.
 */
public class NewsFragment extends Fragment {
    private View view;
    private TextView titleView;

    private XRecyclerView recyclerView;
    private List<News> newsList;
    private NewsFragmentAdapter newsFragmentAdapter;

    private int limit = 8;
    private int skip = 8;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_news, container, false);
        initView();
//        DataUntils.getNewsData(Model.NEWS_URL);
        return view;
    }


    private void initView() {
        titleView = (TextView) view.findViewById(R.id.title);
        recyclerView = (XRecyclerView) view.findViewById(R.id.recyclerView);

        titleView.setText("新闻");
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                BmobQuery<News> query = new BmobQuery<>();
                query.order("createdAt");
                query.setLimit(limit);
                query.findObjects(new FindListener<News>() {
                    @Override
                    public void done(List<News> list, BmobException e) {
                        if (e == null) {
                            for (News newsData : list) {
                                newsList.add(newsData);
                            }
//                            ToastUtil.toast(getActivity(), "已经加载了最新的数据");
                            recyclerView.refreshComplete();
                            newsFragmentAdapter.notifyDataSetChanged();
                        } else {
                            ToastUtil.toast(getActivity(), e.getMessage());
                        }
                    }
                });
            }

            @Override
            public void onLoadMore() {
                BmobQuery<News> query = new BmobQuery<>();
                query.order("createdAt");
                query.setSkip(skip);
                query.setLimit(limit);
                query.findObjects(new FindListener<News>() {
                    @Override
                    public void done(List<News> list, BmobException e) {
                        if (e == null) {
                            limit = limit + 8;
                            skip = skip + 8;
                            for (News newsData : list) {
                                newsList.add(newsData);
                            }
                            recyclerView.loadMoreComplete();
                            newsFragmentAdapter.notifyDataSetChanged();
                        } else {
                            ToastUtil.toast(getActivity(), e.getMessage());
                        }
                    }
                });
            }
        });
        recyclerView.setRefreshing(true);

        newsList = new ArrayList<>();
        newsFragmentAdapter = new NewsFragmentAdapter(getActivity(), newsList);
        recyclerView.setAdapter(newsFragmentAdapter);

        newsFragmentAdapter.OnItemClick(new NewsFragmentAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                News news = newsList.get(position);
                String contentUrl = news.getUrl();
                Intent intent = new Intent(getActivity(), NewsContentActivity.class);
                intent.putExtra("contentUrl", contentUrl);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
